using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
using System.Xml;

public partial class manage_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ParseControls();
        BindInfo();
    }
    private void ParseControls()
    {
        XsltArgumentList xslArg = new XsltArgumentList();
        XPathDocument xdoc = new XPathDocument(Server.MapPath("~/admin/xml/info.xml"));
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(Server.MapPath("~/admin/xml/DynamicControls.xslt"));
        StringWriter sw = new StringWriter();
        transform.Transform(xdoc, xslArg, sw);
        string result = sw.ToString();
        result = result.Replace("xmlns:asp=\"remove\"", "");
        Control ctrl = Page.ParseControl(result);
        phTest.Controls.Add(ctrl);
    }
    private void BindInfo()
    {
        Button btnSaveAddress = (Button)phTest.FindControl("btnSaveAddress");
        btnSaveAddress.Click += new EventHandler(btnSaveAddress_Click);
    }
    protected void btnSaveAddress_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("../admin/xml/info.xml"));
        XmlNodeList nodeList = doc.SelectNodes("/root");
        int ind1 = 1;
        int ind2 = 1;
        foreach (XmlNode parentNode in nodeList)
        {
            XmlNode sectionItem = parentNode.SelectSingleNode("info");
            ind2 = 1;
            foreach (XmlNode node in sectionItem.ChildNodes)
            {
                string ctrlId = node.Attributes.GetNamedItem("Caption").Value + "_" + ind1 + ind2;
                ctrlId = ctrlId.Replace(' ', '_');
                string ctrlType = node.Attributes.GetNamedItem("Type").Value;
                if (ctrlType.Equals("ImgFile"))
                {
                    object ctrl = phTest.FindControl(ctrlId);
                    //string oldFileName = ctrl.ToString;
                    FileUpload imgFile = ctrl as FileUpload;
                    if (imgFile.HasFile)
                    {
                        string path = Server.MapPath("../images/");
                        imgFile.SaveAs(path + imgFile.FileName);
                        node.InnerText = "./images/" + imgFile.FileName;

                        //Delete old file                        
                        object ctrlHidden = phTest.FindControl("hidden" + ind1);
                        HiddenField hidden = ctrlHidden as HiddenField;
                        System.IO.File.Delete(Server.MapPath(hidden.Value));
                    }
                }
                else
                {
                    object ctrl = phTest.FindControl(ctrlId);
                    TextBox txtbox = ctrl as TextBox;
                    node.InnerText = txtbox.Text;
                }
                ind1 += 1;
                ind2 += 1;
            }
        }
        doc.Save(Server.MapPath("../admin/xml/info.xml"));

        Response.Redirect("./Default.aspx");
    }
}
